const { fileModels } = require("../../models"); //导入数据库操作模块dbModels.js
const mkdir = require("../../lib/mkdir"); //导入文件操作模块mkdir.js
//新建文件
exports.uploadFile = async (data, res) => {
    try {
      await fileModels.uploadFile(data)
      .then((result) => {
        let value ={
          ...data,
          ...{id:result.insertId}
        }
       res.send({
         code: 200,
         data: value
       });
      })
    } catch (error) {
      res.send({
        code: 400,
        data: error,
      })
    }
  }
  
  // 根据图片ID删除文件
  exports.deleteFile = async (req, res) => {
    try {
      let data = req.body;
      // 构建正确的本地文件路径
      await fileModels.deleteFile(data.fileId);
      if(typeof data.fileUrl === 'string'){
        const filePath = `data/files/${data.fileUrl}`;
         await mkdir.delFiles(filePath);
      }else{
        data.fileUrl.forEach(async (url) => {
          const filePath = `data/files/${url}`;
             await mkdir.delFiles(filePath);
        });
      }
      res.send({
        code: 200,
        data: { message: '文件删除成功', filePath: data.fileUrl }
      });
    } catch (error) {
      console.error('删除文件错误:', error);
      res.send({
        code: 400,
        data: { message: '删除文件失败', error: error.message }
      })
    }
  }

  // 根据图片url删除文件
  exports.deleteFileByUrl = async (req, res) => {
    try {
      let data = req.body;
      // 构建正确的本地文件路径
      await fileModels.deleteFileByPath(data.fileUrl);
      if(typeof data.fileUrl === 'string'){
        const filePath = `data/files/${data.fileUrl}`;
         await mkdir.delFiles(filePath);
      }else{
        data.fileUrl.forEach(async (url) => {
          const filePath = `data/files/${url}`;
             await mkdir.delFiles(filePath);
        });
      }
      res.send({
        code: 200,
        data: { message: '文件删除成功', filePath: data.fileUrl }
      });
    } catch (error) {
      console.error('删除文件错误:', error);
      res.send({
        code: 400,
        data: { message: '删除文件失败', error: error.message }
      })
    }
  }
  //获取文件
  exports.getFilePage = async (req, res) => {
    try {
      let data = req.body;
      let count = -1 ;
      let result = await fileModels.getFilePage(data.pageSize, data.nowPage,data.subsetId);
      if(data.count){
        let c = await fileModels.getFileCount(data.subsetId);
        count =c[0].count
      }
      res.send({
        code: 200,
        data: {count,result}
      });
    } catch (error) {
      console.error('获取文件错误:', error);
      res.send({
        code: 400,
        data: { message: '获取文件失败', error: error.message }
      })
    }
  }

  //移动文件
  exports.removeFile = async (req, res) => {
    try {
      let data = req.body;
      await fileModels.removeFile(data.fileId,data.subsetId);
      res.send({
        code: 200,
        data: { message: '文件移动成功' }
      });
    } catch (error) {
      console.error('移动文件错误:', error);
      res.send({
        code: 400,
        data: { message: '移动文件失败', error: error.message }
      })
    }
  }
